1 package edu.jiangxin.apktoolbox.file.password.recovery.category.dictionary.multithread; 2 3 import edu.jiangxin.apktoolbox.file.password.recovery.RecoveryPanel; 4 import edu.jiangxin.apktoolbox.file.password.recovery.category.ICategory; 5 import edu.jiangxin.apktoolbox.utils.Utils; 6 import org.apache.logging.log4j.LogManager; 7 import org.apache.logging.log4j.Logger; 8 9 import javax.swing.*; 10 import java.io.File; 11 12 public class DictionaryMultiThreadProxy implements ICategory { 13 private static final Logger logger = LogManager.getLogger(DictionaryMultiThreadProxy.class.getSimpleName()); 14 15 private BigFileReader bigFileReader; 16 17 private String password; 18 19 private final Object lock = new Object(); 20 21 private static class DictionaryMultiThreadProxyHolder { 22 private static final DictionaryMultiThreadProxy instance = new DictionaryMultiThreadProxy(); 23 } 24 25 private DictionaryMultiThreadProxy() { 26 } 27 28 public static DictionaryMultiThreadProxy getInstance() { 29 return DictionaryMultiThreadProxy.DictionaryMultiThreadProxyHolder.instance; 30 } 31 32 private String startAndGet(RecoveryPanel panel) { 33 CompleteCallback callback = password -> { 34 synchronized (lock) { 35 DictionaryMultiThreadProxy.this.password = password; 36 lock.notifyAll(); 37 } 38 }; 39 bigFileReader = new BigFileReader(callback, panel); 40 bigFileReader.start(); 41 try { 42 synchronized (lock) { 43 lock.wait(); 44 } 45 } catch (InterruptedException e) { 46 logger.error("wait InterruptedException"); 47 Thread.currentThread().interrupt(); 48 } 49 return password; 50 } 51 52 @Override 53 public void start(RecoveryPanel panel) { 54 File dictionaryFile = panel.getDictionaryFile(); 55 if (!dictionaryFile.isFile()) { 56 JOptionPane.showMessageDialog(panel, "Invalid dictionary file"); 57 return; 58 } 59 int fileLineCount = Utils.getFileLineCount(dictionaryFile); 60 logger.info("File line count: {}", fileLineCount); 61 62 panel.resetProgressMaxValue(fileLineCount); 63 64 String resultPassword = startAndGet(panel); 65 panel.showResultWithDialog(resultPassword); 66 } 67 68 @Override 69 public void cancel() { 70 if (bigFileReader != null) { 71 bigFileReader.shutdown(); 72 } 73 } 74 }